package com.sky.controller.user;


import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//地址管理
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {


    @Autowired
    private AddressBookService addressBookService;


    /**
     * 查询登录用户所有地址
     * @return
     */
    @ApiOperation("查询登录用户所有地址")
    @GetMapping("/list")
    public Result list(){

        return Result.success(addressBookService.list());
    }


    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }


    /**
     * 查询默认地址
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result getDfault(){
addressBookService.getDfault();
        return Result.success(addressBookService.getDfault());
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result getId(Integer id){
        return Result.success(addressBookService.getId(id));
    }



    @ApiOperation("修改地址")
   @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);

        return Result.success();
    }


    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result deleteId(Integer id){
        addressBookService.deleteId(id);

        return Result.success();
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result updateDefault(@RequestBody AddressBook addressBook){

        addressBookService.updateDefault(addressBook);
        return Result.success();
    }

}
